package com.xindaoapp.happypet.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.CommentListAdapter;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.Comment;
import com.xindaoapp.happypet.bean.CommentEntity;
import com.xindaoapp.happypet.bean.CommentList;
import com.xindaoapp.happypet.utils.xdConfig;
import com.xindaoapp.happypet.view.PullToRefreshListView;
import com.xindaoapp.happypet.view.PullToRefreshListView.OnPullDownRefreshListener;

/**
 * 评价列表
 * 
 * @author Administrator
 */
@SuppressWarnings("unused")
public class CommentListActivity extends UMBaseActivity implements OnClickListener {
    private final String TAG = "CommentListActivity";
    private RelativeLayout no_data;
    private ImageButton cancle;
    private TextView tv_title;
    private TextView tab_1;
    private TextView tab_2;
    private TextView tab_3;
    private TextView tab_4;

    private PullToRefreshListView listView1;
    private PullToRefreshListView listView2;
    private PullToRefreshListView listView3;
    private PullToRefreshListView listView4;

    private CommentListAdapter mAdapter1;
    private CommentListAdapter mAdapter2;
    private CommentListAdapter mAdapter3;
    private CommentListAdapter mAdapter4;

    private FrameLayout loading;
    private final List<Comment> list1 = new ArrayList<Comment>();
    private final List<Comment> list2 = new ArrayList<Comment>();
    private final List<Comment> list3 = new ArrayList<Comment>();
    private final List<Comment> list4 = new ArrayList<Comment>();
    private int currentIndex = 1;
    private boolean loadMore = false;

    private Context context;
    private String productId;
    private String type = "0";

    private final int pageNum1 = 1;
    private final int pageNum2 = 1;
    private final int pageNum3 = 1;
    private final int pageNum4 = 1;

    private final int totalCount1 = 0;
    private final int totalCount2 = 0;
    private final int totalCount3 = 0;
    private final int totalCount4 = 0;
    private final int[] pageNums = {pageNum1, pageNum2, pageNum3, pageNum4};
    private final int[] totalCounts = {totalCount1, totalCount2, totalCount3, totalCount4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commentlist);
        context = CommentListActivity.this;
        productId = getIntent().getStringExtra("productID");

        initView();
        initData();
    }

    private void initView() {
        no_data = (RelativeLayout) findViewById(R.id.no_data);
        cancle = (ImageButton) findViewById(R.id.cancel);
        tv_title = (TextView) findViewById(R.id.check_club);

        tab_1 = (TextView) findViewById(R.id.tab_1);
        tab_2 = (TextView) findViewById(R.id.tab_2);
        tab_3 = (TextView) findViewById(R.id.tab_3);
        tab_4 = (TextView) findViewById(R.id.tab_4);
        tab_1.setOnClickListener(this);
        tab_2.setOnClickListener(this);
        tab_3.setOnClickListener(this);
        tab_4.setOnClickListener(this);

        loading = (FrameLayout) findViewById(R.id.loading);

        cancle.setOnClickListener(this);
        tv_title.setText("商品评价");

        listView1 = (PullToRefreshListView) findViewById(R.id.listview1);
        listView2 = (PullToRefreshListView) findViewById(R.id.listview2);
        listView3 = (PullToRefreshListView) findViewById(R.id.listview3);
        listView4 = (PullToRefreshListView) findViewById(R.id.listview4);
    }

    private void initData() {
        tab_1.setSelected(true);
        getData(productId);
        listView1.setonRefreshListener(new OnPullDownRefreshListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMoring() {
                pageNums[currentIndex - 1]++;
                loadMore = true;
                int limit = xdConfig.PAGE_SIZE;
                if ((pageNums[0] - 1) * limit > totalCounts[currentIndex - 1]) {
                    listView1.onRefreshFinish();
                    return;
                }
                getData(productId);
            }
        });

        listView1.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (listView1.getChildAt(0) != null && listView1.getChildAt(0).getTop() == 0) {// 说明到最顶部了
                    listView1.setSelectionFromTop(0, 0);// 回到header;
                }
            }
        });
        listView1.setRefreshable(false);

        listView2.setonRefreshListener(new OnPullDownRefreshListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMoring() {
                pageNums[currentIndex - 1]++;
                loadMore = true;
                int limit = xdConfig.PAGE_SIZE;
                if ((pageNums[1] - 1) * limit > totalCounts[currentIndex - 1]) {
                    listView2.onRefreshFinish();
                    return;
                }
                getData(productId);
            }
        });
        listView2.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (listView2.getChildAt(0) != null && listView2.getChildAt(0).getTop() == 0) {// 说明到最顶部了
                    listView2.setSelectionFromTop(0, 0);// 回到header;
                }
            }
        });
        listView2.setRefreshable(false);

        listView3.setonRefreshListener(new OnPullDownRefreshListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMoring() {
                pageNums[currentIndex - 1]++;
                loadMore = true;
                int limit = xdConfig.PAGE_SIZE;
                if ((pageNums[2] - 1) * limit > totalCounts[currentIndex - 1]) {
                    listView3.onRefreshFinish();
                    return;
                }
                getData(productId);
            }
        });
        listView3.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (listView3.getChildAt(0) != null && listView3.getChildAt(0).getTop() == 0) {// 说明到最顶部了
                    listView3.setSelectionFromTop(0, 0);// 回到header;
                }
            }
        });
        listView3.setRefreshable(false);

        listView4.setonRefreshListener(new OnPullDownRefreshListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMoring() {
                pageNums[currentIndex - 1]++;
                loadMore = true;
                int limit = xdConfig.PAGE_SIZE;
                if ((pageNums[3] - 1) * limit > totalCounts[currentIndex - 1]) {
                    listView4.onRefreshFinish();
                    return;
                }
                getData(productId);
            }
        });
        listView4.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (listView4.getChildAt(0) != null && listView4.getChildAt(0).getTop() == 0) {// 说明到最顶部了
                    listView4.setSelectionFromTop(0, 0);// 回到header;
                }
            }
        });
        listView4.setRefreshable(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_1:
                if (currentIndex == 1) {
                    return;
                }
                no_data.setVisibility(View.GONE);
                currentIndex = 1;
                tab_1.setSelected(true);
                tab_2.setSelected(false);
                tab_3.setSelected(false);
                tab_4.setSelected(false);
                type = "0";
                pageNums[currentIndex - 1] = 1;
                listView1.setVisibility(View.VISIBLE);
                listView2.setVisibility(View.GONE);
                listView3.setVisibility(View.GONE);
                listView4.setVisibility(View.GONE);
                if (null != mAdapter1) {
                    mAdapter1.clearAllData();
                }
                getData(productId);
                break;
            case R.id.tab_2:
                if (currentIndex == 2) {
                    return;
                }
                no_data.setVisibility(View.GONE);
                currentIndex = 2;
                tab_1.setSelected(false);
                tab_2.setSelected(true);
                tab_3.setSelected(false);
                tab_4.setSelected(false);
                type = "1";
                pageNums[currentIndex - 1] = 1;
                listView1.setVisibility(View.GONE);
                listView2.setVisibility(View.VISIBLE);
                listView3.setVisibility(View.GONE);
                listView4.setVisibility(View.GONE);
                if (null != mAdapter2) {
                    mAdapter2.clearAllData();
                }
                getData(productId);
                break;
            case R.id.tab_3:
                if (currentIndex == 3) {
                    return;
                }
                no_data.setVisibility(View.GONE);
                currentIndex = 3;
                tab_1.setSelected(false);
                tab_2.setSelected(false);
                tab_3.setSelected(true);
                tab_4.setSelected(false);
                type = "2";
                pageNums[currentIndex - 1] = 1;
                listView1.setVisibility(View.GONE);
                listView2.setVisibility(View.GONE);
                listView3.setVisibility(View.VISIBLE);
                listView4.setVisibility(View.GONE);
                if (null != mAdapter3) {
                    mAdapter3.clearAllData();
                }
                getData(productId);
                break;
            case R.id.tab_4:
                if (currentIndex == 4) {
                    return;
                }
                no_data.setVisibility(View.GONE);
                currentIndex = 4;
                tab_1.setSelected(false);
                tab_2.setSelected(false);
                tab_3.setSelected(false);
                tab_4.setSelected(true);
                type = "3";
                pageNums[currentIndex - 1] = 1;
                listView1.setVisibility(View.GONE);
                listView2.setVisibility(View.GONE);
                listView3.setVisibility(View.GONE);
                listView4.setVisibility(View.VISIBLE);
                if (null != mAdapter4) {
                    mAdapter4.clearAllData();
                }
                getData(productId);
                break;
            case R.id.cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void getData(String pId) {
        no_data.setVisibility(View.GONE);
        final RequestParams params = new RequestParams();
        params.put("page", String.valueOf(pageNums[currentIndex - 1]));
        params.put("pagesize", xdConfig.PAGE_SIZE + "");
        params.put("goodsID", productId);
        params.put("type", type);
        Log.e("type------->", type);
        Log.e("currentIndex------->", currentIndex + "");

        // final ProgressHUD progressHUD = ProgressHUD.show(context, "正在加载...", true, true,
        // null);
        AddClient.get(Address.COMMENT_LIST, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                /*
                 * if (null != progressHUD && progressHUD.isShowing()) { progressHUD.dismiss(); }
                 */
                try {
                    if (loadMore) {
                        listView1.onRefreshFinish();
                        listView2.onRefreshFinish();
                        listView3.onRefreshFinish();
                        listView4.onRefreshFinish();
                    }
                    if (TextUtils.isEmpty(content)) {
                        return;
                    }

                    CommentEntity entity = null;
                    int status = -1;
                    String msg = null;
                    try {
                        entity = JSON.parseObject(content, CommentEntity.class);
                        status = JSON.parseObject(content).getInteger("status");
                        msg = JSON.parseObject(content).getString("msg");
                    } catch (Exception e) {
                    }

                    if (status == 0) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (null == entity) {
                        Toast.makeText(context, R.string.get_error_data, 0).show();
                        return;
                    }

                    CommentList info = entity.data;
                    if (null == info) {
                        if (pageNums[currentIndex - 1] == 1) {
                            no_data.setVisibility(View.VISIBLE);
                        }
                        return;
                    }
                    List<Comment> list = info.info;

                    switch (currentIndex) {
                        case 1:
                            totalCounts[currentIndex - 1] = Integer.parseInt(info.count);
                            if (null == mAdapter1) {
                                mAdapter1 =
                                        new CommentListAdapter(CommentListActivity.this, list1,
                                                R.layout.activity_comment_list_item);
                                if (list.size() != 0) {
                                    mAdapter1.addData(list);
                                } else {
                                    if (mAdapter1.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                listView1.addHeaderView(initHeadView(info));
                                listView1.setAdapter(mAdapter1);
                            } else {
                                if (list.size() != 0) {
                                    mAdapter1.addData(list);
                                } else {
                                    if (mAdapter1.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                mAdapter1.notifyDataSetChanged();
                            }
                            break;
                        case 2:
                            totalCounts[currentIndex - 1] = Integer.parseInt(info.count);
                            if (null == mAdapter2) {
                                mAdapter2 =
                                        new CommentListAdapter(CommentListActivity.this, list2,
                                                R.layout.activity_comment_list_item);
                                if (list.size() != 0) {
                                    mAdapter2.addData(list);
                                } else {
                                    if (mAdapter2.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                listView2.setAdapter(mAdapter2);
                            } else {
                                if (list.size() != 0) {
                                    mAdapter2.addData(list);
                                } else {
                                    if (mAdapter2.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                mAdapter2.notifyDataSetChanged();
                            }
                            break;
                        case 3:
                            totalCounts[currentIndex - 1] = Integer.parseInt(info.count);
                            if (null == mAdapter3) {
                                mAdapter3 =
                                        new CommentListAdapter(CommentListActivity.this, list3,
                                                R.layout.activity_comment_list_item);
                                if (list.size() != 0) {
                                    mAdapter3.addData(list);
                                } else {
                                    if (mAdapter3.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                listView3.setAdapter(mAdapter3);
                            } else {
                                if (list.size() != 0) {
                                    mAdapter3.addData(list);
                                } else {
                                    if (mAdapter3.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                mAdapter3.notifyDataSetChanged();
                            }
                            break;
                        case 4:
                            totalCounts[currentIndex - 1] = Integer.parseInt(info.count);
                            if (null == mAdapter4) {
                                mAdapter4 =
                                        new CommentListAdapter(CommentListActivity.this, list4,
                                                R.layout.activity_comment_list_item);
                                if (list.size() != 0) {
                                    mAdapter4.addData(list);
                                } else {
                                    if (mAdapter4.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                listView4.setAdapter(mAdapter4);
                            } else {
                                if (list.size() != 0) {
                                    mAdapter4.addData(list);
                                } else {
                                    if (mAdapter4.getCount() == 0) {
                                        no_data.setVisibility(View.VISIBLE);
                                    }
                                }
                                mAdapter4.notifyDataSetChanged();
                            }
                            break;

                        default:
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinish() {
                if (loading != null && loading.getVisibility() == View.VISIBLE) {
                    loading.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                /*
                 * if (null != progressHUD && progressHUD.isShowing()) { progressHUD.dismiss(); }
                 */
                Toast.makeText(context, R.string.get_error_data, 0).show();
            }

        });
    }

    private View initHeadView(CommentList info) {
        View view = View.inflate(context, R.layout.comment_head, null);
        TextView good_comment = (TextView) view.findViewById(R.id.good_comment);
        TextView good = (TextView) view.findViewById(R.id.good);
        TextView mid = (TextView) view.findViewById(R.id.middle);
        TextView bad = (TextView) view.findViewById(R.id.bad);

        good_comment.setText(info.percent);
        good.setText(info.good_count);
        mid.setText(info.ok_count);
        bad.setText(info.bad_count);
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
